<?php

require_once(dirname(__FILE__) . '/appconf.php');

class dbMove extends Site {

    function setup() {
        $this->run_modes('dbMoveCategory');
        $this->start_mode('dbMoveCategory');
        $this->selfurl = '/dbMoveCategory.php';
        $this->pagesize = 200;
        parent::setup();
    }

    function cgiapp_prerun($rm = '') {
        $this->Points = getModel('Model_Points');
        parent::cgiapp_prerun();
    }

    function action_dbMoveCategory() {

        $page = $this->get_q('page', '1', 'int');

        $param = array();
        $param['field'] = "";
        $param['where'] = "topid<>0 and status=1";
        $param['order'] = "id asc";
        $param['group'] = "";
        $param['limit'] = "";
        $param['pagesize'] = $this->pagesize;
        $param['page'] = $page;
        $param['join'] = "";
        $param['on'] = "";
        $data = db_query_hash('product_layout_category', $param);

        if ($data) {

            if ($page > $data['pageinfo']['pagecount']) {
//echo '<meta http-equiv="refresh" content="1;url=dbMoveFollow.php?action=follow">';
                echo '完成全部轉換 follow';
                exit();
            }
            if ($data['result']) {
                foreach ($data['result'] as $value) {
                    $count = db_query_count('product', "product_layout_category=" . mysql_escape_string($value['id']) . " AND `is_trial` =1 AND `is_live` =1 AND `status` =1 and member_type=5 and reports>0");
                    db_update('product_layout_category', array('products_expert_num' => $count), 'id=' . mysql_escape_string($value['id']));
                    
                    $count = db_query_count('product', "product_layout_category=" . mysql_escape_string($value['id']) . " AND `is_trial` =1 AND `is_live` =1 AND `status` =1 and member_type=1 and reports>0");
                    db_update('product_layout_category', array('products_trialist_num' => $count), 'id=' . mysql_escape_string($value['id']));
                    
                }
            }

            echo '<meta http-equiv="refresh" content="1;url=dbMoveCategory.php?action=dbMoveCategory&page=' . ($page + 1) . '">';
            echo '開始ID ' . $data['result']['0']['id'] . ' 完成本頁，繼續下頁';
        } else {
            echo "無數據";
        }
    }

}

header("Content-type: text/html; charset=utf-8");
$webapp = new dbMove();
$webapp->run();
?>